Component({
  data: {
    selected: 0,
    color: "#999999",
    selectedColor: "#d00000",
    list: [{
      pagePath: "/pages/index/index",
      text: "主页",
      iconClass: "icon-home"
    }, {
      pagePath: "/pages/profile/profile",
      text: "我的",
      iconClass: "icon-my"
    }]
  },
  methods: {
    switchTab(e) {
      const data = e.currentTarget.dataset;
      const url = data.path;
      
      wx.switchTab({
        url
      });
      
      this.setData({
        selected: data.index
      });
    }
  },
  lifetimes: {
    attached: function() {
      // 获取当前页面路径
      const pages = getCurrentPages();
      if (pages.length) {
        const currentPage = pages[pages.length - 1];
        const route = '/' + currentPage.route;
        
        // 根据当前路径设置选中状态
        const list = this.data.list;
        const selected = list.findIndex(item => item.pagePath === route);
        
        if (selected !== -1) {
          this.setData({ selected });
        }
      }
    }
  }
}) 